import {
	IParam,
	IUIActionResult,
	IActionModel,
	IContext,
	IAbility,
	IHttpResponse,
	deepCopy,
	IViewAbility,
	srfpluralize,
	hasFunction,
	formatUrlString,
	IRedirectInput,
	transformDoData,
} from '@/core';
import { Http } from '@/http';
import { EntityUIServiceBase } from '../entity-ui-service-base';

/**
 * 统一认证/外部认证UI服务对象基类
 * 基于template/src/ui-service/{{appEntities}}/{{appEntities}}-ui-service-base.ts.hbs生成
 *
 * @export
 * @class AUTH_PROVIDERUIServiceBase
 * @extends {UIService}
 */
export class AUTH_PROVIDERUIServiceBase extends EntityUIServiceBase {
	/**
	 * Creates an instance of AUTH_PROVIDERUIServiceBase
	 *
	 * @param {*} [opts={}]
	 * @memberof AUTH_PROVIDERUIServiceBase
	 */
	constructor(opts: IParam = {}) {
		super(opts);
		this.appDeKeyFieldName = 'id';
		// 是否启用主状态
		this.isEnableDEMainState = false;
		this.allViewMap.set('EDITVIEW:oauth2', {
			viewName: 'auth_providerSSOEditView',
			viewType: 'DEEDITVIEW',
			srfappde: 'auth_providers',
			deCodeName: 'AUTH_PROVIDER',
		});
		this.allViewMap.set('EDITVIEW:api', {
			viewName: 'auth_providerServiceApiEditView',
			viewType: 'DEEDITVIEW',
			srfappde: 'auth_providers',
			deCodeName: 'AUTH_PROVIDER',
		});
		this.allViewMap.set(':', {
			viewName: 'auth_providerFormPickupDataView',
			viewType: 'DEFORMPICKUPDATAVIEW',
			srfappde: 'auth_providers',
			deCodeName: 'AUTH_PROVIDER',
		});
		this.allViewMap.set('EDITVIEW:', {
			viewName: 'auth_providerEditView',
			viewType: 'DEEDITVIEW',
			srfappde: 'auth_providers',
			deCodeName: 'AUTH_PROVIDER',
		});
		this.allViewMap.set('MDATAVIEW:', {
			viewName: 'auth_providerGridView',
			viewType: 'DEGRIDVIEW',
			srfappde: 'auth_providers',
			deCodeName: 'AUTH_PROVIDER',
		});
		this.allViewMap.set('EDITVIEW:ldap', {
			viewName: 'auth_providerLdapEditView',
			viewType: 'DEEDITVIEW',
			srfappde: 'auth_providers',
			deCodeName: 'AUTH_PROVIDER',
		});
		this.allViewMap.set('FORMPICKUPVIEW:', {
			viewName: 'auth_providerFormPickupView',
			viewType: 'DEPICKUPVIEW',
			srfappde: 'auth_providers',
			deCodeName: 'AUTH_PROVIDER',
		});
	}

	/**
	 * 获取指定数据的重定向页面
	 *
	 * @param context 应用上下文
	 * @param viewParams 视图参数
	 * @param data 业务数据
	 * @param redirectParam 重定向视图输入参数
	 * @memberof AUTH_PROVIDERUIServiceBase
	 */
	public async getAppRDView(
		context: IContext,
		viewParams: IParam,
		data: IParam,
		redirectParam: IRedirectInput
	) {
		// 进行数据查询
		const service = await App.getDataService('AUTH_PROVIDER', context);
		const tempContext: IContext = deepCopy(context);
		const key: string = 'auth_provider';
		let srfkey: string | null = null;
		if (data && data.hasOwnProperty(key)) {
			srfkey = data[key];
		} else if (context && context[key]) {
			srfkey = context[key];
		} else if (viewParams && viewParams[key]) {
			srfkey = viewParams[key];
		}
		Object.assign(tempContext, { [key]: srfkey });
		let result: any;
		if (
			redirectParam.redirectAction &&
			service[redirectParam.redirectAction] &&
			service[redirectParam.redirectAction] instanceof Function
		) {
			result = await service[redirectParam.redirectAction](tempContext, data);
		} else {
			result = await service.Get(tempContext, data);
		}
		const curData: any = result.data;
		// 判断当前数据模式,默认为true,todo
		const iRealDEModel: boolean = true;
		let bDataInWF: boolean = false;
		let bWFMode: any = false;
		if (redirectParam.isEnableWorkflow) {
			return this.getWFView();
		} else {
			if (
				viewParams &&
				viewParams.srfwf &&
				this.InWorkflowArray.indexOf(viewParams.srfwf) !== -1
			) {
				return this.getWFView();
			}
		}
		let strPDTViewParam: string = await this.getDESDDEViewPDTParam(
			curData,
			bDataInWF,
			bWFMode
		);
		return this.allViewMap.get(strPDTViewParam);
	}

	/**
	 * 获取实体单数据实体视图预定义参数
	 *
	 * @param curData 当前数据
	 * @param bDataInWF 是否有数据在工作流中
	 * @param bWFMode   是否工作流模式
	 * @memberof AUTH_PROVIDERUIServiceBase
	 */
	public async getDESDDEViewPDTParam(
		curData: any,
		bDataInWF: boolean,
		bWFMode: boolean
	) {
		let strPDTParam: string = '';
		if (bDataInWF) {
			// TODO 判断数据是否在流程中
		}
		// TODO 多表单
		const multiFormDEField: string | null = 'authentication_type';

		const isApp = App.getProjectSetting().isApp;
		if (multiFormDEField) {
			const objFormValue: string = curData[multiFormDEField];
			if (isApp) {
				return 'MOBEDITVIEW:' + objFormValue;
			}
			return 'EDITVIEW:' + objFormValue;
		}
		if (isApp) {
			if (this.getDEMainStateTag(curData)) {
				return `MOBEDITVIEW:MSTAG:${this.getDEMainStateTag(curData)}`;
			}
			return 'MOBEDITVIEW:';
		}
		if (this.getDEMainStateTag(curData)) {
			return `EDITVIEW:MSTAG:${this.getDEMainStateTag(curData)}`;
		}
		return 'EDITVIEW:';
	}
}
